package com.yzc.mihuyou.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yzc.mihuyou.entity.Comment;
import com.yzc.mihuyou.entity.Pingive;
import com.yzc.mihuyou.mapper.PingiveMapper;
import com.yzc.mihuyou.service.CommentService;
import com.yzc.mihuyou.service.PingiveService;
import com.yzc.mihuyou.service.UserService;
import com.yzc.mihuyou.service.impl.PingiveServiceImpl;
import com.yzc.mihuyou.vo.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yzc
 * @since 2022-05-04
 */
@RestController
@RequestMapping("/mihuyou/pingive")
public class PingiveController {
    @Autowired
    PingiveService pingiveService;

    @Autowired
    UserService userService;

    @Autowired
    PingiveMapper pingiveMapper;

    @Autowired
    CommentService commentService;
    @PostMapping("/likeInform")//点赞查询
    public JsonResult a(long id){
        List<Comment> list = pingiveService.likeInform(id);
        if(list.size()<=0){
            return new JsonResult(500,"出现异常");
        }
        for(Comment com : list){
            com.setUserIds(userService.cuw((Long) com.getUserIds()));
        }
        Map map = new HashMap<>();
        map.put("data",list);
        return new JsonResult(200,"查询成功",map);
    }


    @PostMapping("/addLink")
    public  JsonResult addLink(@RequestBody Pingive pingive){
        if(!pingive.getPortsw()){
            pingiveMapper.insert(pingive);
            commentService.upIue(pingive.getPinzan(),true);
        }else {
            QueryWrapper<Pingive> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("pinzan",pingive.getPinzan());
            queryWrapper.eq("user_id",pingive.getUserId());
            pingiveMapper.delete(queryWrapper);
            commentService.upIue(pingive.getPinzan(),false);
        }
        return  new JsonResult(200,"成功");
    }
}
